<template>
  <el-dialog v-model="materialItemEditWinDialogVisible"
             title="修改产品物料信息" width="50%">
    <template #default>
      <el-form v-model="materialItem" label-width="auto">
        <el-form-item label="产品物料编码">
          <el-input v-model="materialItem.materialId"/>
        </el-form-item>
        <el-form-item label="产品物料名称">
          <el-input v-model="materialItem.materialName"/>
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input v-model="materialItem.Model"/>
        </el-form-item>
        <el-form-item label="最低检测数">
          <el-input v-model="materialItem.testLow"/>
        </el-form-item>
        <el-form-item label="单位">
          <el-input v-model="materialItem.unit"/>
        </el-form-item>
        <el-form-item label="最大不合格数">
          <el-input v-model="materialItem.numberTop"/>
        </el-form-item>
        <el-form-item label="最大致命缺陷率">
          <el-input v-model="materialItem.fatal"/>
        </el-form-item>
        <el-form-item label="最大严重缺陷率">
          <el-input v-model="materialItem.severe"/>
        </el-form-item>
        <el-form-item label="最大轻微缺陷率">
          <el-input v-model="materialItem.slight"/>
        </el-form-item>

      </el-form>
    </template>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelMaterialItemEdit">取消</el-button>
        <el-button @click="editmaterialItemSubmit">确定</el-button>
      </div>
    </template>

  </el-dialog>
</template>
<script setup>
import {ref , watch} from 'vue'
import {ElMessage} from "element-plus";
import materialItemRequest from "@/api/materialItem.js";


//声明这个子组件
const emits = defineEmits(['editMaterialItemCompletes','cancelMaterialItemEdit']);
//申明这个子组件的属性
const props = defineProps(['materialId']);
//弹窗标记
let materialItemEditWinDialogVisible = ref(false);
//编辑的项目对象
let materialItem = ref({});

//取消编辑按钮事件
function cancelMaterialItemEdit(){
  materialItem = ref({});
  materialItemEditWinDialogVisible.value = false;
  // 触发暴漏出去的取消编辑的事件
  emits("cancelMaterialItemEdit")

}
//提交事件
function editmaterialItemSubmit(){
  materialItemRequest.updateMaterialItem(materialItem.value,res=>{
    if(res.code == 200){
      ElMessage({
        type:"success",
        message:"编辑成功"
      });
      // 触发编辑完成的事件
      emits('editMaterialItemCompletes');
      // 关闭弹窗
      cancelMaterialItemEdit();
    }else{
      ElMessage({
        type:"warning",
        message:"项目编辑失败"
      });
    }
  })
}

//加载项目列表
function loadMaterialItem(){
  materialItemRequest.selectById(props.materialId, res=>{
    if (res.data){
      //加载数据
      materialItem.value = res.data;
      console.log(materialItem.value);
      //显示弹窗
      materialItemEditWinDialogVisible.value = true;
    }
  })
}
//侦听器
watch(()=>props.materialId,()=>{
  console.log(props.materialId)
  //当props中的defectId发生变化时，就重新加载数据
  if(props.materialId){
    loadMaterialItem();
  }
})




</script>